<?php

namespace common\models\cms;

use Yii;
use common\helpers\Url;
use common\helpers\StringHelper;

/**
 * This is the model class for table "{{%cms_article}}".
 *
 * @property int $id
 * @property int $channel_id 频道ID
 * @property string $title 标题
 * @property string|null $thumb 封面图
 * @property string|null $keywords 关键词
 * @property string|null $description 导读简介
 * @property int $duration 视频时长
 * @property int $picture_nums 组图数量
 * @property string|null $author 撰稿人
 * @property int $author_id 撰稿人ID
 * @property int $listorder 排序
 * @property int $publish_at 发布时间
 * @property int $recommend_at 推荐时间
 * @property int $views 浏览次数
 * @property int $likes 点赞数
 * @property int $weight 权重
 * @property int $status 状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 删除时间
 */
class CmsArticle extends \common\db\ActiveRecord {

    use \common\traits\Constants;

    /**
     * 文章状态
     */
    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;

    /**
     * 是否开启评论
     */
    const ALLOW_COMMENT_YES = 1;
    const ALLOW_COMMENT_NO = 0;

    /**
     * 文章格式
     */
    const FORMAT_DEFAULT = 'default';
    const FORMAT_GALLERY = 'gallery';
    const FORMAT_VIDEO = 'video';
    const FORMAT_AUDIO = 'audio';
    const FORMAT_CALENDAR = 'calendar';

    /**
     * 文章格式
     * 无图
     */
    const TEMPLATE_TEXT = 'text';
    const TEMPLATE_THUMB = 'thumb';
    const TEMPLATE_BANNER = 'banner';
    const TEMPLATE_GRID = 'grid';

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%cms_article}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['channel_id', 'duration', 'picture_nums', 'author_id', 'listorder', 'publish_at', 'recommend_at', 'views', 'likes', 'weight', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['title'], 'required'],
            [['title'], 'string', 'max' => 80],
            [['thumb', 'keywords', 'description'], 'string', 'max' => 255],
            [['author'], 'string', 'max' => 20],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'channel_id' => '频道ID',
            'title' => '标题',
            'thumb' => '封面图',
            'keywords' => '关键词',
            'description' => '导读简介',
            'duration' => '视频时长',
            'picture_nums' => '组图数量',
            'author' => '撰稿人',
            'author_id' => '撰稿人ID',
            'listorder' => '排序',
            'publish_at' => '发布时间',
            'recommend_at' => '推荐时间',
            'views' => '浏览次数',
            'likes' => '点赞数',
            'weight' => '权重',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'deleted_at' => '删除时间',
            //
            'format' => '资讯类型',
        ];
    }

    /**
     * 关联文章详情
     */
    public function getAdminer() {
        return $this->hasOne(\common\models\admin\AdminUser::class, ['id' => 'author_id']);
    }

    /**
     * 关联文章详情
     */
    public function getChannel() {
        return $this->hasOne(CmsChannel::class, ['id' => 'channel_id']);
    }

    /**
     * 关联详情
     */
    public function getData() {
        return $this->hasOne(CmsArticleData::class, ['article_id' => 'id']);
    }

    /**
     * 关联图集
     */
    public function getAttachs() {
        return $this->hasMany(CmsArticleAttach::class, ['article_id' => 'id']);
    }

    /**
     * 商品检索关键字
     * @return \common\db\ActiveQuery
     */
    public function getKeywords() {
        return $this->hasMany(\common\models\keyword\KeywordRelation::class, ['model_id' => 'id'])->andWhere([
            'model' => \common\models\keyword\KeywordRelation::MODEL_ARTICLE
        ]);
    }

    /**
     * 关联图集
     */
    public function getAlbum() {
        return $this->getAttachs()->andWhere([
            'format' => 0,
        ])->orderBy([
            'format' => SORT_DESC,
            'listorder' => SORT_ASC,
        ]);
    }

    public function getLiked() {
        return $this->hasOne(CmsArticleLike::class, ['article_id' => 'id'])->andWhere([
            'user_id' => Yii::$app->user->id
        ])->exists();
    }

    /**
     * 关联视频
     */
    public function getVideo() {
        return $this->hasOne(CmsArticleAttach::class, ['article_id' => 'id'])->andWhere([
            'format' => 1,
        ]);
    }

    /**
     * 关联统计信息
     */
    public function getPreviewUrl() {
        $route = [
            'preview'
        ];
        $route[] = 'article';
        $route[] = $this->id;
        return Url::h5(implode('/', $route));
    }

    /**
     * 关联统计信息
     */
    public function getPublishTime() {
        return date('Y-m-d H:i:s', $this->publish_at ?: $this->created_at);
    }

    public function getTags() {
        return array_filter(StringHelper::splitWords($this->keywords));
    }

    /**
     * 关联文章详情
     */
    public function getFormat() {
        if ($this->video) {
            return 'video';
        }
        return 'default';
    }

    /**
     * 小程序跳转地址
     * @return bool
     */
    public function getMplink() {
        return Url::to(['/pages/show/index', 'id' => $this->id]);
    }

    public function growUpViews() {
        $this->views += 1;
        $this->weight = $this->views + $this->likes;
        return $this->save();
    }

}
